package com.info33.platform.system.dic.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonView;
import com.info33.platform.common.mvc.controller.BaseController;
import com.info33.platform.common.mvc.vo.Result;
import com.info33.platform.system.dic.dto.SysDicDTO;
import com.info33.platform.system.dic.entity.SysDic;
import com.info33.platform.system.dic.service.SysDicService;
import com.info33.platform.system.dic.vo.SysDicVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统 - 字典 v2
 *
 * @author nyj2one
 * @since 2022-11-23 11:08:37
 */
@RestController
@RequestMapping("/SysDicContent")
@Api(tags = "系统 - 字典 v2")
public class SysDicController extends BaseController<SysDicService, SysDic> {

    private final SysDicService sysDicService;

    @Autowired
    public SysDicController(SysDicService sysDicService) {
        super(sysDicService);
        this.sysDicService = sysDicService;
    }

    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * PC 端 api，可能有用户权限的加入……
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */

    @PostMapping(value = "/pc/page/{pageNum}/{pageSize}")
    @ApiOperation(
            value = "pc 分页",
            notes = "-1-1 查询全部")
    public Result<IPage<SysDicVO>> pcPage(
            @JsonView(SysDicDTO.PcPage.class)
            @RequestBody SysDicDTO dto,
            @PathVariable int pageNum,
            @PathVariable int pageSize) {
        return Result.ok(sysDicService.pcPage(new Page<>(pageNum, pageSize), dto));
    }

    @PostMapping(value = "/pc/add")
    @ApiOperation(value = "pc 新增")
    public Result<Object> pcAdd(
            @JsonView(SysDicDTO.PcAdd.class)
            @Validated(SysDicDTO.PcAdd.class)
            @RequestBody SysDicDTO dto) {
        return sysDicService.pcAdd(dto);
    }

    @PutMapping(value = "/pc/edit")
    @ApiOperation(value = "pc 修改")
    public Result<Object> pcEdit(
            @JsonView(SysDicDTO.PcEdit.class)
            @RequestBody SysDicDTO dto) {
        return sysDicService.pcEdit(dto);
    }

    @DeleteMapping(value = "/pc/remove/{id}")
    @ApiOperation(value = "pc 删除")
    public Result<Object> pcRemove(
            @PathVariable Long id) {
        return sysDicService.pcRemove(id);
    }

    @GetMapping(value = "/pc/tree/{modIndex}")
    @ApiOperation(value = "pc 树结构")
    @JsonView(SysDicVO.PcTree.class)
    public Result<List<SysDicVO>> pcTree(
            @PathVariable String modIndex) {
        return Result.ok(sysDicService.pcTree(modIndex));
    }

    @GetMapping(value = "/pc/list/{modIndex}")
    @ApiOperation(value = "pc 模块下所有字典列表")
    @JsonView(SysDicVO.PcList.class)
    public Result<List<SysDicVO>> pcList(
            @PathVariable String modIndex) {
        return Result.ok(sysDicService.pcList(modIndex));
    }

    @GetMapping(value = "/pc/listByMods/{modIndexes}")
    @ApiOperation(value = "pc 获取多个索引的字典列表")
    @JsonView(SysDicVO.PcListByMods.class)
    public Result<List<List<SysDicVO>>> pcListByMods(
            @PathVariable String... modIndexes) {
        return Result.ok(sysDicService.pcListByMods(modIndexes));
    }

    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * Open 开放 api，会带有租户校验...
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */

    @PostMapping(value = "/open/list")
    @ApiOperation(value = "open 列表")
    @JsonView(SysDicVO.OpenList.class)
    public Result<List<SysDicVO>> openList(
            @JsonView(SysDicDTO.OpenList.class)
            @Validated(SysDicDTO.OpenList.class)
            @RequestBody SysDicDTO dto) {
        return Result.ok(sysDicService.openList(dto));
    }

    @PostMapping(value = "/open/tree")
    @ApiOperation(value = "open 树结构")
    @JsonView(SysDicVO.OpenTree.class)
    public Result<List<SysDicVO>> openTree(
            @JsonView(SysDicDTO.OpenTree.class)
            @Validated(SysDicDTO.OpenTree.class)
            @RequestBody SysDicDTO dto) {
        return Result.ok(sysDicService.openTree(dto));
    }

    @PostMapping(value = "/open/listByMods/{modEnumNames}")
    @ApiOperation(value = "open 获取多个索引的字典列表")
    @JsonView(SysDicVO.OpenListByMods.class)
    public Result<List<List<SysDicVO>>> openListByMods(
            @JsonView(SysDicDTO.PcListByMods.class)
            @Validated(SysDicDTO.PcListByMods.class)
            @RequestBody SysDicDTO dto,
            @PathVariable String... modEnumNames) {
        return Result.ok(sysDicService.openListByMods(dto, modEnumNames));
    }

}
